package forces;

import java.awt.geom.Point2D;
import physicalobjects.Mass;

/**
 * Creates the center of mass force.
 * @author Kathleen Lan, Seon Kang
 */

public class CenterOfMass extends Force {

    private static double ourDEFAULTDIRECTION = 0.0;
    private static int ourDEFAULTMAGNITUDE = 1;
    private static double ourDEFAULTEXPONENT = 0.0;

    private double myExponentValue;
 // either equal to DEFAULT_MAGNITUDE or
    // magnitude inputed from Environmental data
    // file
 // current magnitude (equals 0 when
    // CenterOfMass is toggled off,
    // equal to baseMagnitude when CenterOfMass
    // is toggled on)
    private boolean myRepulsive = false;
    private String myForceName = "centermass";

    /**
     *
     */
    public CenterOfMass () {
        super(ourDEFAULTDIRECTION, ourDEFAULTMAGNITUDE);
        myExponentValue = ourDEFAULTEXPONENT;
        setMyName(myForceName);
    }

    /**
     * @param m 
     * @param e 
     */
    public CenterOfMass (int m, double e) {
        if (m < 0) {
            myRepulsive = true;
            setMagnitude(-1 * m);
            setBaseMagnitude(-1 * m);
        }
        else {
            setMagnitude(m);
            setBaseMagnitude(m);
        }
        myExponentValue = e;
        setMyName(myForceName);
    }

    @Override
    public void update (Mass m) {
        // calculates and sets direction
        Point2D massCenter = m.getCenter();
        double myDirection = angleBetween(massCenter, m.getCenterOfMass());
        setDirection(myDirection);
        if (myRepulsive) {
            negate();
        }
        // calculates and sets magnitude
        double myDistance = massCenter.distance(m.getCenterOfMass());
        setMagnitude(getMagnitude() / Math.pow(myDistance, myExponentValue));
        // applies force to Mass m
        m.applyForce(this);
    }

}
